package com.fayuan.nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fayuan.nocv.entity.HeSuan;
import com.fayuan.nocv.entity.User;
import com.fayuan.nocv.service.HesuanService;
import com.fayuan.nocv.service.UserService;
import com.fayuan.nocv.vo.DataView;
import com.fayuan.nocv.vo.HesuanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/hesuan")
public class HesuanController {

    @Autowired
    private HesuanService hesuanService;
    @Autowired
    private UserService userService;

    @GetMapping("/loadAllHeSuan")
    @ResponseBody
    public DataView loadAllHeSuan(HesuanVo hesuanVo){

        IPage<HeSuan>page = new Page<>();
        QueryWrapper<HeSuan>wrapper = new QueryWrapper<>();
        if (hesuanVo.getUsername()!=null){
            wrapper.like("name",hesuanVo.getUsername());
        }
        IPage<HeSuan> page1 = hesuanService.page(page, wrapper);

        List<HeSuan> records = page1.getRecords();

        for (HeSuan record : records) {
            if (record.getDoctorId()!=null){
                User byId = userService.getById(record.getDoctorId());
                record.setDoctor(byId.getUsername());
            }
        }

        return new DataView(page1.getTotal(),page1.getRecords());
    }

    @GetMapping("/loadMyHeSuan")
    @ResponseBody
    public DataView loadMyHesuan(HesuanVo hesuanVo, HttpSession session){

        IPage<HeSuan>page = new Page<>(hesuanVo.getPage(),hesuanVo.getLimit());
        QueryWrapper<HeSuan>wrapper = new QueryWrapper<>();
        User user = (User) session.getAttribute("user");
        wrapper.eq("username",user.getUsername());
        IPage<HeSuan> page1 = hesuanService.page(page, wrapper);

        List<HeSuan> records = page1.getRecords();

        for (HeSuan record : records) {
            if (record.getDoctorId()!=null){
                User byId = userService.getById(record.getDoctorId());
                record.setDoctor(byId.getUsername());
            }
        }

        return new DataView(page1.getTotal(),page1.getRecords());
    }

    @PostMapping("/addHeSuan")
    @ResponseBody
    public DataView addHeSuan(HeSuan heSuan) throws ParseException {

        Date createTime = heSuan.getCreateTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(createTime);
        Date parse = dateFormat.parse(format);

        heSuan.setCreateTime(parse);

        hesuanService.save(heSuan);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg(heSuan.getUsername()+"添加成功");
        return dataView;
    }

    @PostMapping("/updateHeSuan")
    @ResponseBody
    public DataView updateHesuan(HeSuan heSuan) throws ParseException {

        Date createTime = heSuan.getCreateTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(createTime);
        Date parse = dateFormat.parse(format);

        heSuan.setCreateTime(parse);

        hesuanService.updateById(heSuan);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg(heSuan.getUsername()+"修改成功");
        return dataView;
    }

    @PostMapping("/deleteHeSuan")
    @ResponseBody
    public DataView deleteHesuan(HeSuan heSuan){

        hesuanService.removeById(heSuan.getId());
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg(heSuan.getUsername()+"删除成功");
        return dataView;
    }
    @GetMapping("/listAllDoctor")
    @ResponseBody
    public List<User> listAllDoctor(){

        QueryWrapper<User>wrapper = new QueryWrapper<>();
        wrapper.isNotNull("department_id");
        wrapper.isNotNull("office_id");
       return userService.list(wrapper);
    }
}
